package cn.ililes.tools.nosql.tests.springs.impls;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import cn.ilikes.tools.nosql.test.jpa.pojos.Cycles;
import cn.ilikes.tools.nosql.test.jpa.pojos.EmployeeGroup;
import cn.ililes.tools.nosql.tests.springs.ICycleService;

@Component("ICycleService")
public class CycleService implements ICycleService {

	@PersistenceContext(name = "ilikes")
	private EntityManager entityManager;

	@Override
	public void addCycle(Cycles cycles) {
		entityManager.persist(cycles);

	}

	@Override
	public List<Cycles> selectCyclesByName(String name) {

		Query query = entityManager.createQuery("select cycles  from  Cycles  as  cycles    where  cycles.cyclesName =':wq' ");
		query.setParameter("wq", name);
		List<Cycles> cycleses = query.getResultList();
		for (Cycles cycle : cycleses) {
			System.out.println("=======================  122222   " + cycle.getUuid() + "   " + cycle.getCyclesName() + "   " + cycle.getEmployeeGroups().size());
		}
		return null;

	}

	@Override
	public List<Cycles> selectQueryCycleByQueryName(String name) {
		Query query = entityManager.createNamedQuery("testcycles");
		query.setParameter("wq", name);
		List<Cycles> cycleses = query.getResultList();
		for (Cycles cycle : cycleses) {
			System.out.println("=======================  122222   " + cycle.getUuid() + "   " + cycle.getCyclesName() + "   " + cycle.getEmployeeGroups().get(0).getGroupName());
		}
		return cycleses;

	}

	@Override
	public List<EmployeeGroup> selectEmployeeGroupByCycleId(String name) {
		Query query = entityManager.createQuery("select  employeeGroup  from  EmployeeGroup  as  employeeGroup  " + "    where  employeeGroup.cycles  in  (':wq')    ");
		query.setParameter("wq", name);
		List<EmployeeGroup> results = query.getResultList();
		for (EmployeeGroup employeeGroup : results) {
			System.out.println("=======================  12211212 222   " + employeeGroup);
		}
		return null;
	}

	@Override
	public Cycles getCycleBYId(Object Id) {
		return entityManager.find(Cycles.class, Id);

	}

	@Override
	public void delCycleById(Object Id) {
		Cycles cycle = entityManager.find(Cycles.class, Id);
		entityManager.remove(cycle);
	}

	@Override
	public List<EmployeeGroup> selectEmployeeGroupByName(String name) {
		Query query = entityManager.createQuery("select  employeeGroup  from  EmployeeGroup  as  employeeGroup  " + "    where  employeeGroup.groupName  = ':wq'     ");
		query.setParameter("wq", name);
		query.setFirstResult(10);
		query.setMaxResults(2);
		List<EmployeeGroup> results = query.getResultList();
		int i = 0;
		for (EmployeeGroup employeeGroup : results) {
			if (employeeGroup.getCycles() != null)
				System.out.println("=================" + (i++) + " ======  12211212 222   " + employeeGroup.getCycles().getCyclesName());
			else
				System.out.println("=======================  12211212 222  null   " + employeeGroup.getCycles());
		}
		return null;
	}

}
